package ${package.Controller};

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import com.example.infrastructure.common.Result;
import com.example.infrastructure.common.PageResult;
import com.example.infrastructure.excel.ExcelUtils;
import com.example.infrastructure.lock.DistributedLockUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>
import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import java.util.List;

/**
 * ${table.comment!} 控制器
 *
 * @author ${author}
 * @since ${date}
 */
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@RequestMapping("<#if package.ModuleName?? && package.ModuleName != "">/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
@Tag(name = "${table.comment!}管理", description = "${table.comment!}相关接口")
@Validated
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
</#if>

    @Autowired
    private ${table.serviceName} ${table.entityPath}Service;
    
    @Autowired
    private DistributedLockUtils distributedLockUtils;

    /**
     * 创建${table.comment!}
     */
    @PostMapping
    @Operation(summary = "创建${table.comment!}")
    public Result<Boolean> create(@Valid @RequestBody ${entity} ${table.entityPath}) {
        return Result.success(${table.entityPath}Service.save(${table.entityPath}));
    }

    /**
     * 根据ID删除${table.comment!}
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除${table.comment!}")
    public Result<Boolean> delete(@Parameter(description = "主键ID") @PathVariable @NotNull Long id) {
        return Result.success(${table.entityPath}Service.removeById(id));
    }

    /**
     * 批量删除${table.comment!}
     */
    @DeleteMapping("/batch")
    @Operation(summary = "批量删除${table.comment!}")
    public Result<Boolean> deleteBatch(@RequestBody List<Long> ids) {
        return Result.success(${table.entityPath}Service.removeByIds(ids));
    }

    /**
     * 更新${table.comment!}
     */
    @PutMapping
    @Operation(summary = "更新${table.comment!}")
    public Result<Boolean> update(@Valid @RequestBody ${entity} ${table.entityPath}) {
        return Result.success(${table.entityPath}Service.updateById(${table.entityPath}));
    }

    /**
     * 根据ID查询${table.comment!}
     */
    @GetMapping("/{id}")
    @Operation(summary = "查询${table.comment!}详情")
    public Result<${entity}> getById(@Parameter(description = "主键ID") @PathVariable @NotNull Long id) {
        return Result.success(${table.entityPath}Service.getById(id));
    }

    /**
     * 分页查询${table.comment!}
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询${table.comment!}")
    public Result<PageResult<${entity}>> page(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Long current,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Long size) {
        Page<${entity}> page = new Page<>(current, size);
        Page<${entity}> result = ${table.entityPath}Service.page(page);
        return Result.success(PageResult.of(result));
    }

    /**
     * 查询所有${table.comment!}
     */
    @GetMapping("/list")
    @Operation(summary = "查询所有${table.comment!}")
    public Result<List<${entity}>> list() {
        return Result.success(${table.entityPath}Service.list());
    }

    /**
     * 导出${table.comment!}
     */
    @GetMapping("/export")
    @Operation(summary = "导出${table.comment!}")
    public void export(HttpServletResponse response) {
        List<${entity}> list = ${table.entityPath}Service.list();
        ExcelUtils.export(response, "${table.comment!}数据", "${table.comment!}", ${entity}.class, list);
    }

    /**
     * 导入${table.comment!}
     */
    @PostMapping("/import")
    @Operation(summary = "导入${table.comment!}")
    public Result<String> importData(@RequestParam("file") MultipartFile file) {
        return distributedLockUtils.executeWithLock("import:${table.entityPath}", () -> {
            List<${entity}> list = ExcelUtils.importExcel(file, ${entity}.class, null);
            boolean success = ${table.entityPath}Service.saveBatch(list);
            return success ? Result.success("导入成功，共" + list.size() + "条数据") 
                          : Result.error("导入失败");
        });
    }

    /**
     * 下载导入模板
     */
    @GetMapping("/template")
    @Operation(summary = "下载导入模板")
    public void downloadTemplate(HttpServletResponse response) {
        List<${entity}> templateData = List.of(new ${entity}());
        ExcelUtils.export(response, "${table.comment!}导入模板", "${table.comment!}", ${entity}.class, templateData);
    }
}
